/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.wechat.entity.MassMedia;
import com.boxin.ims.modules.wechat.entity.MemberScore;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.dao.MassMediaDao;

/**
 * 群发素材管理Service
 * @author XiaoQian
 * @version 2014-03-28
 */
@Component
@Transactional(readOnly = true)
public class MassMediaService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(MassMediaService.class);
	
	@Autowired
	private MassMediaDao massMediaDao;
	
	public MassMedia get(Long id) {
		return massMediaDao.findOne(id);
	}
	
	public Page<MassMedia> find(Page<MassMedia> page, MassMedia massMedia) {
		DetachedCriteria dc = massMediaDao.createDetachedCriteria();
		if (massMedia.getUser()!=null && massMedia.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", massMedia.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(massMedia.getTitle())){
			dc.add(Restrictions.like("title", "%"+massMedia.getTitle()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", MassMedia.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return massMediaDao.find(page, dc);
	}
	
	
	public List<MassMedia> findByUserId(Long UserId) {
		
		DetachedCriteria dc = massMediaDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id",UserId));
		List<MassMedia> massMedia=massMediaDao.find(dc);
		return massMedia;
	}
	
	@Transactional(readOnly = false)
	public void save(MassMedia massMedia) {
		massMediaDao.save(massMedia);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		massMediaDao.deleteById(id);
	}
	
}
